package cn.jhz.learn.blog.controller.security;

import cn.jhz.learn.blog.common.bean.JsonData;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class SecurityExceptionHandler {
    /**
     * 账号错误<br>
     * 密码错误<br>
     * 无此账号<br>
     * @param exception
     * @return
     */
    @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
    @ExceptionHandler(
            value = {
                    UnknownAccountException.class, IncorrectCredentialsException.class, AuthenticationException.class
            })
    public JsonData loginFailedHandle(Exception exception){
        return JsonData.fail("账号或密码错误");
    }

    /**
     * 账号被锁定
     * @param exception
     * @return
     */
    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(value = {LockedAccountException.class})
    public JsonData accountLocked(Exception exception){
        return JsonData.fail("账号被锁定");
    }

    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(value = AuthorizationException.class)
    public JsonData notAuthorized(Exception exception){
        return JsonData.fail("行为未被授权!");
    }
}
